from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.utils import Bunch
import matplotlib.pyplot as plt
from sklearn.metrics import classification_report

iris:Bunch = datasets.load_iris()
print(type(iris))
X = iris.data[:100, :]

y = iris.target[:100]
print("========")
print(type(X),type(y))
print(iris.feature_names)
# 画图

plt.scatter(X[:50,0],X[:50,1],c='r',label='Seatosa')
plt.scatter(X[50:,0],X[50:,1],c='black',label='Versicolour')
plt.ylabel("Sepal.Width")
plt.xlabel('Sepal.Length')
plt.legend()
plt.show()

plt.scatter(X[:50,2],X[:50,3],c='r',label='Seatosa')
plt.scatter(X[50:,2],X[50:,3],c='blue', label='Versicolour')
plt.ylabel("Petal.Width")
plt.xlabel('Petal.Length')
plt.legend()
plt.show()

X_train,X_test,y_train,y_test = train_test_split(X, y, test_size=0.3)
log_reg = LogisticRegression()
log_reg.fit(X_train,y_train)
r2_score = log_reg.score(X_test, y_test)
print(r2_score)
# 验证
index= 51
real = iris.target[index]
print(X[index,:])
predict = log_reg.predict([X[index,:]])
print(f"真实值: {real},预测值: {predict}")
# TODO 这块有点不好理解
print(f"回归项系数w: {log_reg.coef_}")
print(f"偏移量b: {log_reg.intercept_}")

print(classification_report(y_test, log_reg.predict(X_test)))